/**
 * Code parsing utilities for formatting and displaying source code
 */

/**
 * Parse a Vue file content into its components
 * @param {string} content - The raw Vue file content
 * @returns {Object} Parsed components: template, script, and style
 */
export function parseVueFile(content) {
  const result = {
    template: '',
    script: '',
    style: ''
  };

  // Extract template section
  const templateMatch = content.match(/<template>([\s\S]*)<\/template>/i);
  if (templateMatch) {
    result.template = templateMatch[1].trim();
  }

  // Extract script section
  const scriptMatch = content.match(/<script>([\s\S]*)<\/script>/i);
  if (scriptMatch) {
    result.script = scriptMatch[1].trim();
  }

  // Extract style section
  const styleMatch = content.match(/<style[^>]*>([\s\S]*)<\/style>/i);
  if (styleMatch) {
    result.style = styleMatch[1].trim();
  }

  return result;
}

/**
 * Generate related file paths for a demo
 * @param {string} demoId - The demo ID
 * @returns {Array} Array of related file objects
 */
export function getRelatedFiles(demoId) {
  const relatedFiles = [];
  
  // Map of demos that have related utility files
  const demoUtilsMap = {
    'polygon-animation-demo': [
      {
        name: 'PolygonAnimationDemo.utils.js',
        path: '/src/demos/PolygonAnimationDemo.utils.js'
      }
    ]
  };
  
  // Add main Vue file for all demos
  relatedFiles.push({
    name: `${getComponentName(demoId)}.vue`,
    path: `/src/demos/${getComponentName(demoId)}.vue`,
    type: 'vue'
  });
  
  // Add related utility files if they exist
  if (demoUtilsMap[demoId]) {
    relatedFiles.push(...demoUtilsMap[demoId]);
  }
  
  return relatedFiles;
}

/**
 * Convert demo ID to component name
 * @param {string} demoId - The demo ID
 * @returns {string} Component name
 */
function getComponentName(demoId) {
  // Special case mapping for demo IDs to component names
  const specialCases = {
    'billboard-demo': 'BillboardDemo',
    'camera-controls-demo': 'CameraControlsDemo',
    'component-appearance-customization': 'ComponentAppearanceCustomization',
    'component-basic-usage': 'ComponentBasicUsage',
    'component-custom-styling': 'ComponentCustomStyling',
    'component-visibility-control': 'ComponentVisibilityControl',
    'custom-materials-demo': 'CustomMaterialsDemo',
    'entity-clustering-demo': 'EntityClusteringDemo',
    'label-demo': 'LabelDemo',
    'model-demo': 'ModelDemo',
    'path-animation-demo': 'PathAnimationDemo',
    'point-animation-demo': 'PointAnimationDemo',
    'polygon-animation-demo': 'PolygonAnimationDemo',
    'terrain-demo': 'TerrainDemo',
    'test-demo': 'TestDemo',
    'tileset-demo': 'TilesetDemo'
  };
  
  if (specialCases[demoId]) {
    return specialCases[demoId];
  }
  
  // Default conversion for other demos
  return demoId.split('-')
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join('') + 'Demo';
}

/**
 * Determine file type by extension
 * @param {string} fileName - Name of the file
 * @returns {string} File type
 */
export function getFileType(fileName) {
  if (fileName.endsWith('.vue')) return 'vue';
  if (fileName.endsWith('.js')) return 'javascript';
  if (fileName.endsWith('.css') || fileName.endsWith('.scss') || fileName.endsWith('.less')) return 'css';
  return 'text';
}